<?php

namespace App\Services\admin;

use App\Models\admin\ApplicationSetmeal;
use App\Models\admin\Setmeal;
use App\Services\BaseService;

/**
 * 套餐 Service
 *
 * @Desc:
 *
 * @author: zhx
 *
 * @Time: 2023/8/24 10:37
 */
class SetmealService extends BaseService
{
    /**
     * 分页数据查询
     *
     * @Desc:
     *
     * @return mixed
     *
     * @author: zhx
     *
     * @Time: 2023/8/9 15:04
     */
    public static function getPageData($requestData)
    {
        [$perPage, $page, $orderBy, $orderDir] = [
            isset($requestData['perPage']) ? $requestData['perPage'] : 20,
            isset($requestData['page']) ? $requestData['page'] : 1,
            isset($requestData['orderBy']) ? $requestData['orderBy'] : 'id',
            isset($requestData['orderDir']) ? $requestData['orderDir'] : 'desc',
        ];
        $models = Setmeal::query();
        if (isset($requestData['name'])) {
            $models->where('name', 'like', '%'.$requestData['name'].'%');
        }
        if (isset($requestData['state'])) {
            $models->where('state', $requestData['state']);
        }
        $list = $models->orderBy($orderBy, $orderDir)->paginate($perPage);

        return $list;
    }

    /**
     * 数据查询
     *
     * @Desc:
     *
     * @return mixed
     *
     * @author: zhx
     *
     * @Time: 2023/8/9 15:04
     */
    public static function getData($requestData)
    {
        [$orderBy, $orderDir] = [
            isset($requestData['orderBy']) ? $requestData['orderBy'] : 'id',
            isset($requestData['orderDir']) ? $requestData['orderDir'] : 'desc',
        ];
        $models = ApplicationSetmeal::query();
        if (isset($requestData['name'])) {
            $models->where('name', 'like', '%'.$requestData['name'].'%');
        }
        if (isset($requestData['state'])) {
            $models->where('state', $requestData['state']);
        }
        $list = $models->orderBy($orderBy, $orderDir)->get();

        return $list;
    }

    /**
     * 编辑
     *
     * @Desc:
     *
     * @return \Illuminate\Http\JsonResponse
     *
     * @throws \Illuminate\Validation\ValidationException
     *
     * @author: zhx
     *
     * @Time: 2023/8/10 14:44
     */
    public static function edit($id, $requestData)
    {
        $models = Setmeal::query();
        $models->where('id', $id);

        return $models->update($requestData);
    }

    /**
     * 添加
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface create
     */
    public static function create($requestData)
    {
        return Setmeal::create($requestData);
    }

    /**
     * 详细
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface info
     */
    public static function info($id)
    {
        $models = Setmeal::query();

        return $models->where('id', $id)->first();
    }

    /**
     * 删除
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface info
     */
    public static function destroy($id)
    {
        $models = Setmeal::query();

        return $models->where('id', $id)->update([
            'deleted_at' => date('Y-m-d H:i:s'),
        ]);
    }
}
